export default {
  bind(el, binding) {
    const { value, arg } = binding;
    if (arg === 'copy') {
      el.$handle = () => {
        const inp = document.createElement('input');
        inp.value = el.$value || value;
        inp.style.position = 'absolute';
        inp.style.zIndex = '-999';
        document.body.appendChild(inp);
        inp.select();
        document.execCommand('copy');
        el.$success();
        document.body.removeChild(inp);
      };
      el.addEventListener('click', el.$handle);
    } else if (arg === 'success') {
      el.$success = value;
    }
  },
  // 组件更新，重新取值
  componentUpdated(el, { value, arg }) {
    if (arg === 'copy') {
      el.$value = value;
    }
  },
  unbind(el) {
    el.removeEventListener('click', el.$handle);
  }
};
